#!/bin/bash

echo "=========================================="
echo "  主机巡检功能测试"
echo "=========================================="
echo ""

# 获取Token
echo "1. 获取认证Token..."
cd /aiops/backend
TOKEN=$(python manage.py shell << 'EOF'
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
user = User.objects.filter(is_superuser=True).first()
if user:
    token, _ = Token.objects.get_or_create(user=user)
    print(token.key)
EOF
)

if [ -z "$TOKEN" ]; then
    echo "❌ 未找到管理员用户Token"
    echo "请先创建管理员用户"
    exit 1
fi

echo "✅ Token: $TOKEN"
echo ""

# 测试API
echo "2. 测试巡检API..."
RESPONSE=$(curl -s -X POST http://localhost:8000/api/host-inspection/ \
  -H "Content-Type: application/json" \
  -H "Authorization: Token $TOKEN" \
  -d '{"host_ids":[]}')

echo "$RESPONSE" | python3 -m json.tool 2>/dev/null

if echo "$RESPONSE" | grep -q "total"; then
    echo ""
    echo "✅ 巡检API工作正常！"
    echo ""
    echo "=========================================="
    echo "  使用方法"
    echo "=========================================="
    echo ""
    echo "1. 访问前端："
    echo "   http://localhost:8081/"
    echo ""
    echo "2. 登录系统"
    echo ""
    echo "3. 点击「系统仪表盘」"
    echo ""
    echo "4. 看到「主机巡检」界面"
    echo ""
    echo "5. 点击「开始巡检」按钮"
    echo ""
    echo "🎉 巡检功能已就绪！"
else
    echo ""
    echo "❌ 巡检API测试失败"
    echo "响应内容："
    echo "$RESPONSE"
fi
